package com.chen.blue.controller.admin.business;

import com.chen.blue.domain.delete.business.CompetitionDelete;
import com.chen.blue.domain.delete.business.CompetitionItemDelete;
import com.chen.blue.domain.dto.business.update.CompetitionEdit;
import com.chen.blue.domain.dto.business.update.CompetitionItemEdit;
import com.chen.blue.domain.dto.excel.CompetitionExcel;
import com.chen.blue.domain.query.business.CompetitionQuery;
import com.chen.blue.domain.vo.business.CompetitionVo;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.result.DataResult;
import com.chen.blue.result.R;
import com.chen.blue.service.business.ICompetitionService;
import com.chen.blue.utils.ExcelUtil;
import com.chen.blue.utils.MinioUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * Created By CLT On 2024/6/20.
 */
@SuppressWarnings("all")
@ApiSort(999)
@Api(tags = "赛事赛项管理-赛事赛项模块")
@Validated
@RestController
@RequestMapping("/competition")
public class CompetitionController {

    @Resource
    private ICompetitionService iCompetitionService;

    @ApiOperation(value = "赛事导入模版下载", notes = "赛事导入模版下载")
    @PostMapping("/template/download")
    public void templateDownload(HttpServletResponse response) {

        ClassPathResource resource = new ClassPathResource("excel/赛事导入.xls");
        MinioUtil.download(response, resource);

    }

    @ApiOperation(value = "赛事导入", notes = "赛事导入")
    @PostMapping("/include")
    public R addCompetition(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws IOException, DtoNullException {

        List<CompetitionExcel> include = ExcelUtil.include(file, CompetitionExcel.class, response);
        return iCompetitionService.addCompetition(include) ? R.success() : R.faild();

    }

    @ApiOperation(value = "赛事导出", notes = "赛事导出")
    @PostMapping("/export")
    public void exportCompetition(HttpServletResponse response) {

        List<CompetitionExcel> competitionExcels = iCompetitionService.exportCompetition();
        ExcelUtil.export(response, CompetitionExcel.class, "赛事数据.xls", "赛事数据", competitionExcels);

    }

    @ApiOperation(value = "赛事禁用或启用", notes = "赛事禁用或启用")
    @PutMapping("/competition/forbidden")
    public R forbiddCompetition(@Valid @RequestBody CompetitionEdit edit) throws DtoNullException {

        return iCompetitionService.forbiddenCompetition(edit) ? R.success() : R.faild();

    }

    @ApiOperation(value = "删除赛事(有赛项不可删除)", notes = "删除赛事(有赛项不可删除)")
    @DeleteMapping("/compeition/delete")
    public R deleteCompetition(@Valid @RequestBody CompetitionDelete delete) throws DtoNullException {

        return iCompetitionService.deleteCompetition(delete) ? R.success() : R.faild();

    }

    @ApiOperation(value = "赛项禁用或启用", notes = "赛项禁用或启用")
    @PutMapping("/item/forbidden")
    public R forbiddItem(@Valid @RequestBody CompetitionItemEdit edit) throws DtoNullException {

        return iCompetitionService.forbiddenItem(edit) ? R.success() : R.faild();

    }

    @ApiOperation(value = "删除赛项", notes = "删除赛项")
    @DeleteMapping("/item/delete")
    public R deleteItem(@Valid @RequestBody CompetitionItemDelete delete) throws DtoNullException {

        return iCompetitionService.deleteItem(delete) ? R.success() : R.faild();

    }

    @ApiOperation(value = "赛事及赛项查询：（赛项名称和赛项状态必须配合赛事名称和赛事状态其中一个使用）", notes = "赛事及赛项查询：（赛项名称和赛项状态必须配合赛事名称和赛事状态其中一个使用）")
    @PostMapping("/query")
    public R<DataResult<List<CompetitionVo>>> queryCompetitions(@Valid @RequestBody CompetitionQuery query) throws DtoNullException {

        return R.success(iCompetitionService.competitionList(query));

    }

    @ApiOperation(value = "赛事及赛项下拉选", notes = "赛事及赛项下拉选")
    @GetMapping("/query/select")
    public R<DataResult<List<CompetitionVo>>> queryCompetitionSelect() throws DtoNullException {

        return R.success(iCompetitionService.competitionListSelect());

    }

}
